package com.senjoeson.androidtv.securitydemo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * <pre>
 *     author : sunqiao
 *     e-mail : sunqiao@kayak.com.cn
 *     time   : 2018/10/31
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class FileUtils {

    /**
     * @param filePath 文件名
     * @return
     */
    public static byte[] readFile(String filePath) {
        FileInputStream inputStream = null;
        ByteArrayOutputStream swapStream = null;
        byte[] result = null;
        try {
            if (filePath != null && !"".equals(filePath)) {
                File file = new File(filePath);
                inputStream = new FileInputStream(file);
                swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据
                int rc = 0;
                while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                    swapStream.write(buff, 0, rc);
                }
                result = swapStream.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (swapStream != null) {
                try {
                    swapStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
